package edcom.server;

import edcom.client.Student;

public class LoginView {
	LoginController loginctlr;
	String[] errorMessages;
	
	public LoginView(){
		loginctlr = new LoginController();
		errorMessages = new String[8];
		errorMessages[Student.name] = "";
		errorMessages[Student.studentNumber] = "";
		errorMessages[Student.username] = "";
		errorMessages[Student.password] = "";
		errorMessages[Student.email] = "";
		errorMessages[Student.major] = "";
		errorMessages[Student.sqlStatus] = "Not registered.";
		errorMessages[Student.generalError] = "";
	}
	
	public String[] requestLogin(String username, String password){
		
		boolean CHECK_SUCCESS = true;
		
		try {
			loginctlr.setUsername(username);
		} catch (MalformedStringException e) {
			errorMessages[Student.username] = e.getMessage();
			CHECK_SUCCESS = false;
		}
		
		try {
			loginctlr.setPassword(password);
		} catch (MalformedStringException e) {
			errorMessages[Student.password] = e.getMessage();
			CHECK_SUCCESS = false;
		}
		//CHECK_SUCCESS = true;
		if(CHECK_SUCCESS) {
			Student std = null;
			errorMessages[Student.sqlStatus] = "";
			try {
				std = loginctlr.login();
				errorMessages[Student.name] = std.getName();
				errorMessages[Student.username] = std.getUserName();
				errorMessages[Student.major] = std.getMajor();
				errorMessages[Student.email] = std.getEmail();
			}catch(SQLSelectException e){
				errorMessages[Student.sqlStatus] = e.getMessage();
			}catch(Exception e){
				errorMessages[Student.generalError] = "ERROR: " + e.getMessage();
			}
			if (std == null) ;//errorMessages[Student.sqlStatus] = "Invalid username/password.";
		}
		else {
			errorMessages[Student.generalError] = "Bad strings.";
		}
		
		
		
		return errorMessages;
	}
}
